package test.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;

public class RebuildData {
	
	public static void main(String[] args) {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core2");
		SolrQuery query = new SolrQuery("*:*");
		try {
			QueryResponse qr = client.query(query);
			List<SolrInputDocument> idocs = new ArrayList<>(); 
			for(SolrDocument doc :qr.getResults()) {
				SolrInputDocument idoc = new SolrInputDocument();
				idoc.addField("id", doc.getFieldValue("id"));
				idoc.addField("channel_id", doc.getFieldValue("channel_id"));
				idoc.addField("click_count", doc.getFieldValue("click_count"));
				idoc.addField("creater", doc.getFieldValue("creater"));
				idoc.addField("updater", doc.getFieldValue("updater"));
				idoc.addField("tag_id", doc.getFieldValue("tag_id"));
				idoc.addField("author", doc.getFieldValue("author"));
				idoc.addField("title", doc.getFieldValue("title"));
				idoc.addField("text", doc.getFieldValue("text"));
				idoc.addField("intro", doc.getFieldValue("intro"));
				idoc.addField("ctime", doc.getFieldValue("ctime"));
				
				idocs.add(idoc);
			}
			
			client.add(idocs);
			client.commit();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
